package com.cysyz.web.modules.base.permission.controller;

import com.cysyz.web.modules.base.businesslog.annotation.BusinessLogInfo;
import com.cysyz.web.modules.base.permission.annotation.Module;
import com.cysyz.web.modules.base.permission.annotation.ModuleFunction;
import com.cysyz.web.modules.base.permission.domain.SysRole;
import com.cysyz.web.modules.base.permission.service.PermissionService;
import com.cysyz.web.modules.base.permission.service.RoleService;
import com.cysyz.web.modules.base.permission.support.ModuleFunctionType;
import com.cysyz.web.modules.base.support.controller.SpringMVCBaseController;
import com.cysyz.web.modules.base.user.service.SysUserService;
import edu.emory.mathcs.backport.java.util.Arrays;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/back/role")
@Module(name="角色管理", code="role")
public class SysRoleManageController extends SpringMVCBaseController {
    @Resource
    private RoleService roleService;

    @Resource
    private PermissionService permissionService;

    @Resource
    private SysUserService sysUserService;

    @RequestMapping("/list")
    @ModuleFunction(value = "角色列表", moduleFunctionType = ModuleFunctionType.MENU)
    public String list() {
        //setRequestAttribute("sysRoleList", roleService.list());
        return "back/permission/role/role_list";
    }

    @RequestMapping("/roleListData")
    @ModuleFunction(value = "角色查询", menuPrivilegeMethodName = "list")
    @ResponseBody
    public String search(SysRole sysRole, int start, int length){
        int page = start / length + 1;
        int pageSize = length;
        if (sysRole != null && sysRole.getName() != null) {
            sysRole.setName(transcodingIfGet(sysRole.getName()));
        }
        return responseJSON(roleService.getRoleList(sysRole, page, pageSize));
    }

    @RequestMapping("/editRole")
    @ModuleFunction("角色修改")
    @BusinessLogInfo(operationName = "角色修改")
    public String edit(SysRole sysRole) {
        //setBusineeLogMessage("修改的角色的名称" + sysRole.getName());
        roleService.editRoleInfo(sysRole);
        return "redirect:list.do";
    }

    @RequestMapping("/editRoleUI")
    public String editUI(Long roleId) {
        setRequestAttribute("roleInfo", roleService.get(SysRole.class, roleId));
        return "back/permission/role/role_edit";
    }

    @RequestMapping("/addRoleUI")
    public String addUI() {
        return "back/permission/role/role_add";
    }

    @RequestMapping("/addRole")
    @ModuleFunction("角色添加")
    @BusinessLogInfo(operationName = "角色添加")
    public String add(SysRole role) {
        roleService.addNewRole(role);
        return "redirect:list.do";
    }

    @RequestMapping("/setPrivilegeUI")
    public String setPrivilegeUI(Long roleId) {
        setRequestAttribute("allPrivilege", permissionService.findAllPrivilege(roleId));
        setRequestAttribute("roleId", roleId);
        return "back/permission/role/role_privilege_config";
    }

    @RequestMapping("/setPrivilege")
    @ModuleFunction("分配权限")
    @BusinessLogInfo(operationName = "分配权限")
    public String setPrivilege(Long roleId, Long[] privilegeIds) {

        if (privilegeIds != null && privilegeIds.length > 0) {
            roleService.addPrivilegesForRole(roleId, Arrays.asList(privilegeIds));
        } else {
            roleService.removeAllPrivilegesForRole(roleId);
        }

        return "redirect:list.do";
    }

    @RequestMapping("/setUsersForRoleUI")
    public String setUsersForRoleUI(Long roleId) {
        setRequestAttribute("sysUsers", sysUserService.getAllUserOfRole(roleId));;
        setRequestAttribute("roleId", roleId);;
        return "back/permission/role/role_user_config";
    }


//    @RequestMapping("/setUsersForRole")
//    @ModuleFunction("分配角色")
//    @BusinessLogInfo(operationName = "分配角色")
//    public String setUsersForRole(Long roleId, String sysUsersIdsCsv) {
//
//        if (roleId != null && MyStringUtils.hasText(sysUsersIdsCsv)) {
//            roleService.addUsersForRole(roleId, Arrays.asList(sysUsersIdsCsv.split(",")));
//        } else {
//            roleService.removeAllUserOfRole(roleId);
//        }
//        return "redirect:list.do";
//    }

    @RequestMapping("/validateRoleName")
    @ResponseBody
    public String validateRoleNameExist(@RequestParam("name") String name) {
        name = transcodingIfGet(name);
        return new Boolean(roleService.isRoleNameExists(name)).toString();
    }
}



